package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Shortcuts;
import com.hl.card.core.support.BeanKit;
import com.hl.card.modular.packBack.service.GoodsSevice;
import com.hl.card.modular.packBack.service.IShortcutsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangpu
 * @since 2018-06-25
 */
@Controller
@RequestMapping("/shortcuts")
public class ShortcutsController extends BaseController {

    @Resource
    private IShortcutsService iShortcutsService;

    @Resource
    private GoodsSevice goodsSevice;

    private String PREFIX = "/packback/shortcuts/";

    @RequestMapping("")
    public String index(){
        return PREFIX+"shortcuts.html";
    }

    /**
     * 跳转到添加首页快捷栏
     */
    @RequestMapping(value = "/shortcuts_add")
    public Object shortcutsAdd(Model model){
        List<Map<String, Object>> list = goodsSevice.selectList();
        model.addAttribute("goods",list);
        return PREFIX+"shortcuts_add.html";
    }

    /**
     * 跳转到修改首页快捷栏
     */
    @RequestMapping(value = "/shortcuts_update/{shortcutsId}")
    public Object  shortcutsUpdate(@PathVariable Integer shortcutsId, Model model){
        List<Map<String, Object>> list = goodsSevice.selectList();
        model.addAttribute("goods",list);
        Shortcuts shortcuts = iShortcutsService.selectById(shortcutsId);
        Map<String, Object> shortcutsmap = BeanKit.beanToMap(shortcuts);
        if(shortcuts.getIsCondition() == 1) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            if(!"".equals(shortcuts.getRegister_start_time()) && shortcuts.getRegister_start_time() != null) {
                String regist_start_time = format.format(shortcuts.getRegister_start_time());
                String regist_end_time = format.format(shortcuts.getRegister_end_time());
                shortcutsmap.put("register_start_time", regist_start_time);
                shortcutsmap.put("register_end_time", regist_end_time);
            }
            if( shortcuts.getStart_time() != null && !"".equals(shortcuts.getStart_time())) {
                String end_time = format.format(shortcuts.getEnd_time());
                String start_time = format.format(shortcuts.getStart_time());
                shortcutsmap.put("start_time", start_time);
                shortcutsmap.put("end_time", end_time);
            }
        }
        System.out.println(shortcutsmap);
        model.addAttribute("shortcuts",shortcutsmap);
        return PREFIX+"shortcuts_edit.html";
    }


    /**
     * 获取首页快捷栏列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        List<Shortcuts> list = iShortcutsService.selectList();
        for(int i = 0;i < list.size();i++){
            Shortcuts shortcuts = list.get(i);
            String img = shortcuts.getShortcuts_img();
            shortcuts.setShortcuts_img(img.substring(0,30)+"...");
        }
        return list;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Integer id){
        iShortcutsService.delete(id);
        return super.SUCCESS_TIP;
    }
    /**
     * 添加
     * @param shortcuts
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(String functional_title,String shortcuts_img,String goodsCode,String shortcuts_url,Integer isCondition,String start_time,String end_time,String register_start_time,String register_end_time,String mutex__code) throws ParseException {
        Shortcuts shortcuts1= iShortcutsService.selectByCode(goodsCode);
        if(shortcuts1 == null) {
            Shortcuts shortcuts = new Shortcuts();
            if (!shortcuts_img.equals("")) {
                shortcuts.setShortcuts_img(shortcuts_img);
            } else {
                return super.FailuerTip("图片不能为空");

            }
            if (!functional_title.equals("")) {
                shortcuts.setFunctional_title(functional_title);
            } else {
                return super.FailuerTip("功能标题不能为空");

            }
            if (!goodsCode.equals("")) {
                shortcuts.setGoods_code(goodsCode);
            } else {

                return super.FailuerTip("产品类型不能为空");
            }
            if (!shortcuts_url.equals("")) {
                shortcuts.setShortcuts_url(shortcuts_url);
            } else {
                return super.FailuerTip("地址不能为空");
            }
            shortcuts.setIsCondition(isCondition);
            if (isCondition == 1) {
                if (!start_time.equals("")) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    shortcuts.setStart_time(sdf.parse(start_time));
                    shortcuts.setEnd_time(sdf.parse(end_time));
                }
                if (!register_start_time.equals("")) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    shortcuts.setRegister_start_time(sdf.parse(register_start_time));
                    shortcuts.setRegister_end_time(sdf.parse(register_end_time));
                }
                if (!mutex__code.equals("")) {
                    shortcuts.setMutex__code(mutex__code);
                }
            }
            Date date = new Date();
            shortcuts.setCreate_time(date);
            shortcuts.setLast_update_time(date);
            iShortcutsService.sava(shortcuts);
            return super.SUCCESS_TIP;
        }else {
            return  super.FailuerTip("改产品类型已经添加");
        }
    }
    /**
     * 更新
     * @param shortcuts
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Integer id,String functional_title,String shortcuts_img,String goodsCode,String shortcuts_url,Integer isCondition,String start_time,String end_time,String register_start_time,String register_end_time,String mutex__code) throws ParseException {
        Shortcuts shortcuts = new Shortcuts();
        shortcuts.setId(id);
        shortcuts.setShortcuts_url(shortcuts_url);
        shortcuts.setShortcuts_img(shortcuts_img);
        shortcuts.setIsCondition(isCondition);
        shortcuts.setGoods_code(goodsCode);
        shortcuts.setFunctional_title(functional_title);
        if(isCondition == 1){
            if(!start_time.equals("")){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                shortcuts.setStart_time(sdf.parse(start_time));
                shortcuts.setEnd_time(sdf.parse(end_time));
            }else {
                shortcuts.setStart_time(null);
                shortcuts.setEnd_time(null);
            }
            if(!register_start_time.equals("")){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                shortcuts.setRegister_start_time(sdf.parse(register_start_time));
                shortcuts.setRegister_end_time(sdf.parse(register_end_time));
            }else{
                shortcuts.setRegister_start_time(null);
                shortcuts.setRegister_end_time(null);

            }
            if(!mutex__code.equals("")){
                shortcuts.setMutex__code(mutex__code);
            }else{
                shortcuts.setMutex__code("");
            }
        }else{
            shortcuts.setStart_time(null);
            shortcuts.setEnd_time(null);
            shortcuts.setRegister_start_time(null);
            shortcuts.setRegister_end_time(null);
            shortcuts.setMutex__code("");
        }
        shortcuts.setLast_update_time(new Date());
        iShortcutsService.update(shortcuts);
        return super.SUCCESS_TIP;
    }
    /**
     * 置顶
     * @param id
     * @return
     */
    @RequestMapping(value = "/stick")
    @ResponseBody
    public Object stick(Integer id){
        Shortcuts shortcuts = iShortcutsService.selectById(id);
        shortcuts.setStick_time(new Date());
        iShortcutsService.update(shortcuts);
        return super.SUCCESS_TIP;
    }
}
